package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.cacheable.Cacheable;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.POIUtils;
import com.itheima.utils.QiniuUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.UUID;


/**
 * @author M.F.S
 */
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("member")
public class MemberController {


    @Reference(version = "1.0.0")
    private MemberService memberService;
    @Resource
    private JedisPool jedisPool;

    /** ZS
     * 根据档案number查询member
     * @param id 档案number
     * @return 直接返回member
     */
    @GetMapping("findByFileId")
    public Result findByFileId(Long id){
        Result member = memberService.findByFileId(id);
        if (member == null) {
            return new Result(false, "没有该档案");
        }
        return new Result(true, "查询成功", member);
    }


    /**
     * 分页查询方法
     *
     * @param queryPageBean 分页查询会员实体类
     * @return 分页查询结果，总条数与当前页数据
     */
    @RequestMapping("findPage")
//    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = memberService.findPage(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }

    /**
     * 根据ID查询会员
     *
     * @param id 会员ID
     * @return 返回Result包含member
     */
//    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    @GetMapping("findById/{id}")
    public Result findById(@PathVariable int id) {
        Member member = null;
        try {
            member = memberService.findById(id);
            return new Result(true, "查询会员成功", member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询会员失败");
        }
    }

//    @GetMapping("findById/{id}")
//    public Result findById(int id){
//        Member member = memberService.findById(id);
//        return new Result(true,"查询会员成功",member);
//    }

    /**
     * 编辑
     *
     * @param member 会员实体类
     * @return 编辑操作的结果
     */
    @RequestMapping("update")
//    @PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")
    public Result update(@RequestBody Member member) {
        System.out.println("member = " + member.toString());
        //调用业务层修改方法
        try {
            memberService.update(member);
            //返回操作结果
            return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
        }
    }

    /**
     * 新增会员档案信息
     *
     * @param member 会员
     * @return 操作结果
     */
    @RequestMapping("add")
    public Result add(@RequestBody Member member) {
        //调用业务层添加方法
        try {
            memberService.add(member);
            //返回操作结果
            return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }
    }


    /**
     * 删除
     *
     * @param id 要删除的会员的ID
     * @return 编辑操作的结果
     */
    @GetMapping("delete/{id}")
//    @PreAuthorize("hasAuthority('CHECKGROUP_DELETE')")
    public Result delete(@PathVariable int id) {
        //调用业务层删除方法
        try {
            memberService.delete(id);
            //返回操作结果
            return new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MEMBER_FAIL);
        }
    }

    /**
     * 查询会员信息(会员数据回显)
     *
     * @return
     */
    @GetMapping("findMember")
    public Result findMember() {
        //调用业务层
        try {
            List<Member> list = memberService.findMember();
            //返回操作结果
            return new Result(true, "查询会员成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询会员失败");
        }
    }
    /**
     * 回显会员信息
     * @param id 会员ID
     * @return 返回Result包含member
     */
    @GetMapping("findByIdInfo/{id}")
    public Result findByIdInfo(@PathVariable int id) {
        Member member = null;
        try {
            member = memberService.findByIdInfo(id);
            return new Result(true, "查询会员成功", member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询会员失败");
        }
    }
}
